package com.zhy.generator.codegen.mybatis.javarequest.elements;

import com.zhy.generator.codegen.dom.java.*;
import com.zhy.generator.codegen.mybatis.AbstractJavaGenerator;
import com.zhy.generator.config.Configuration;
import com.zhy.generator.config.ConfigurationConstant;

import java.util.List;

import static cn.hutool.core.util.StrUtil.isNotBlank;

/**
 * @author 源
 */
public abstract class AbstractJavaRequestInnerClassGenerator extends AbstractJavaGenerator {

    public abstract void addInterfaceElements(TopLevelClass tlc);

    public FullyQualifiedJavaType getSuperInterface() {
        String rootInterface = "java.io.Serializable";
        return new FullyQualifiedJavaType(rootInterface);
    }

    protected void addDataAnnotation(TopLevelClass tlc, InnerClass ic) {
        ic.addAnnotation("@Data");
        tlc.addImportedType("lombok.Data");
    }

    protected void addApiModelAnnotation(TopLevelClass tlc, InnerClass ic) {
        String className = tlc.getType().getShortName();
        String innerClassName = ic.getType().getShortName();
        StringBuffer sb = new StringBuffer();
        sb.append(className).append("_").append(innerClassName);
        ic.addAnnotation("@ApiModel(value = \"" + sb.toString() + "\")");
        tlc.addImportedType("io.swagger.annotations.ApiModel");
    }

    protected void addFieldAnnotation(TopLevelClass tlc, Field field, String remarks, boolean required) {
        FullyQualifiedJavaType type = field.getType();
        String shortName = type.getShortName();

        if (isNotBlank(remarks) && remarks.contains("\n")) {
            remarks = remarks.substring(0, remarks.indexOf("\n"));
        }

        if (type.isArray()) {
            field.addAnnotation("");
        } else if (shortName.equals("String")) {
            field.addAnnotation("@NotEmpty(message = \"" + remarks + "不能为空\")");
            tlc.addImportedType("javax.validation.constraints.NotEmpty");
        } else {
            field.addAnnotation("@NotNull(message = \"" + remarks + "不能为空\")");
            tlc.addImportedType("javax.validation.constraints.NotNull");
        }

        if (shortName.equals("Long")) {
            field.addAnnotation("@Min(value = 1, message = \"" + remarks + "输入错误，必须是数字且大于0\")");
            tlc.addImportedType("javax.validation.constraints.Min");
        } else if (shortName.equals("Integer")) {
            field.addAnnotation("@Min(value = 0, message = \"" + remarks + "输入错误，必须是数字且大于等于0\")");
            tlc.addImportedType("javax.validation.constraints.Min");
        }

        field.addAnnotation("@ApiModelProperty(value = \"" + remarks + "\", required = " + required + ")");
        tlc.addImportedType("io.swagger.annotations.ApiModelProperty");

    }

    protected void addSerialVersionUIDField(InnerClass ilc) {
        Field field = new Field();
        field.setName("serialVersionUID");
        field.setType(PrimitiveTypeWrapper.getLongInstance());
        field.setStatic(true);
        field.setFinal(true);
        field.setVisibility(JavaVisibility.PRIVATE);
        long l = System.currentTimeMillis();
        field.setInitializationString(l + "L");
        ilc.addField(field);
    }


    protected FullyQualifiedJavaType getSuperClass() {
        Configuration configuration = context.getConfiguration(ConfigurationConstant.CONFIGURATION_PO);
        if (configuration == null) {
            return null;
        }
        String rootClass = configuration.getRootClass();
        if (isNotBlank(rootClass)) {
            FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(rootClass);
            return fqjt;
        }
        return null;
    }

    @Override
    public List<CompilationUnit> getCompilationUnits() {
        return null;
    }
}
